package com.wss.merchant.configuration;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.wss.merchant.util.JWTUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

@Component
@Slf4j
public class MyInterceptor implements HandlerInterceptor {
    @Autowired
    private JWTUtil jwtUtil;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        if("OPTIONS".equals(request.getMethod().toUpperCase())) {
            return true;
        }
//        System.out.println("执行了TestInterceptor的preHandle方法");
        String token = request.getHeader("token");
        try{
            DecodedJWT decodedJWT = jwtUtil.verifyToken(token);
            return true;
        }catch (Exception e){
            log.info("我被拦截了");
            return false;
        }


    }


}
